package com.syzg.base.role.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.syzg.common.service.impl.BaseServiceImpl;
import com.syzg.base.role.service.RoleService;
import com.syzg.base.role.dao.RoleDAO;
import com.syzg.base.merge.model.Operation;
import com.syzg.base.merge.model.Role;
import com.syzg.base.merge.model.RoleOperation;
import com.syzg.base.merge.model.RoleOperationId;
import com.syzg.base.role.model.RoleView;
import com.syzg.common.exception.BaseException;
import com.syzg.common.util.MethodUtil;
import com.syzg.common.util.SystemUtil;
import com.syzg.core.exception.ServiceException;

import java.util.*;

@Service("roleService")
public class RoleServiceImpl extends BaseServiceImpl<Role> implements RoleService {
    
    private RoleDAO roleDAO = null;

    @Resource
	public void setRoleDAO(RoleDAO roleDAO) {
		this.setDAO(roleDAO);
		this.roleDAO = roleDAO;
	}
    
    public void createRole(Role role, String operationIds[])
        throws ServiceException {
        if(isExist(null, new String[]{"code"}, new String[]{role.getCode()}, Role.class)) {
            throw new BaseException("角色编号已存在，请使用其他编号!", false);
        }
        roleDAO.create(role);
        String along[];
        int j = (along = operationIds).length;
        for(int i = 0; i < j; i++) {
        	String optId = along[i];
            RoleOperation roleOpt = new RoleOperation();
            Operation opt = new Operation();
            opt.setId(optId);
            roleOpt.setRole(role);
            roleOpt.setOperation(opt);
            roleOpt.setId(new RoleOperationId(opt.getId(), role.getId()));
            roleDAO.create(roleOpt);
        }
    }

    public void updateRole(Role role, String opterationIds[]) throws ServiceException {
        if(isExist(role.getId(), new String[]{"code"}, new String[]{role.getCode()}, Role.class)){
        	throw new BaseException("角色编号已存在，请使用其他编号!", false);
        }
        roleDAO.update(role);
        roleDAO.deleteRoleOperationRelation(role.getId());
        String along[];
        int j = (along = opterationIds).length;
        for(int i = 0; i < j; i++)
        {
        	String optId = along[i];
            RoleOperation roleOpt = new RoleOperation();
            Operation opt = new Operation();
            opt.setId(optId);
            roleOpt.setRole(role);
            roleOpt.setOperation(opt);
            roleOpt.setId(new RoleOperationId(opt.getId(), role.getId()));
            roleDAO.create(roleOpt);
        }
    }

    public void deleteRole(String roleIds[]) throws ServiceException {
        String roleNames[] = roleDAO.queryRoleUsed(roleIds);
        if(roleNames != null && roleNames.length > 0){
            throw new BaseException((new StringBuilder("删除的角色[")).append(SystemUtil.getInSql(roleNames)).append("]正在被使用,不能删除还在使用的角色！").toString(), false);
        }
        for(int i = 0; i < roleIds.length; i++)
        {
            roleDAO.deleteRoleOperationRelation(roleIds[i]);
            roleDAO.deleteRole(roleIds[i]);
        }
    }
    
	public List findRoles() throws ServiceException {
        return roleDAO.findRoles();
    }

    public List findRolesByPerson(String personId) throws ServiceException {
        List roleList = roleDAO.findRolesByPerson(personId);
        List returnList = new ArrayList();
        RoleView roleView;
        for(Iterator iterator = roleList.iterator(); iterator.hasNext();) {
            Object object[] = (Object[])iterator.next();
            Role role = (Role)object[0];
            roleView = new RoleView();
            try
            {
                MethodUtil.copyProperties(roleView, role);
                returnList.add(roleView);
            } catch(Exception e) {
                e.printStackTrace();
                throw new BaseException(e);
            }
            roleView.setIsSelected((String)object[1]);
        }
        return returnList;
    }

    public String[] findRoleIdsByPersonId(String personId) throws ServiceException {
        return roleDAO.findRoleIdsByPersonId(personId);
    }
}
